package net.huashitong.giszd.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import net.huashitong.giszd.entity.EarlyWarningNode;
import net.huashitong.giszd.filter.EarlyWarningNodeFilter;
import net.huashitong.giszd.responseModel.EarlyWarningCount;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class EarlyWarningNodeDao extends StringDao<EarlyWarningNode> {

    public List<EarlyWarningNode> queryEarlyWarning(EarlyWarningNodeFilter filter) {
        GenericQuery query = createQuery("obj");
        if (filter.getProjectId() != null && !"".equals(filter.getProjectId()))
            query.append(" and obj.projectId=:projectId").setParam("projectId", filter.getProjectId());
        if (filter.getProjectName() != null && !"".equals(filter.getProjectName()))
            query.append(" and obj.projectName like:projectName").setParam("projectName", "%" + filter.getProjectName() + "%");
        if (filter.getNodeSysId() != null && !"".equals(filter.getNodeSysId()))
            query.append(" and obj.nodeSysId =:nodeSysId").setParam("nodeSysId", filter.getNodeSysId());
        if (filter.getNodeId() != null && !"".equals(filter.getNodeId()))
            query.append(" and obj.nodeId =:nodeId").setParam("nodeId", filter.getNodeId());
        if (filter.getNodeName() != null && !"".equals(filter.getNodeName()))
            query.append(" and obj.nodeName =:nodeName").setParam("nodeName", filter.getNodeName());
        if (filter.getResDept() != null && !"".equals(filter.getResDept()))
            query.append(" and obj.resDept =:resDept").setParam("resDept", filter.getResDept());
        if (filter.getResUser() != null && !"".equals(filter.getResUser()))
            query.append(" and obj.resUser =:resUser").setParam("resUser", filter.getResUser());
        if (filter.getResUserPhone() != null && !"".equals(filter.getResUserPhone()))
            query.append(" and obj.resUserPhone =:resUserPhone").setParam("resUserPhone", filter.getResUserPhone());
        if (filter.getOperatorUser() != null && !"".equals(filter.getOperatorUser()))
            query.append(" and obj.operatorUser =:operatorUser").setParam("operatorUser", filter.getOperatorUser());
        if (filter.getOperatorPhone() != null && !"".equals(filter.getOperatorPhone()))
            query.append(" and obj.operatorPhone =:operatorPhone").setParam("operatorPhone", filter.getOperatorPhone());
        if (filter.getNodeType() != null && !"".equals(filter.getNodeType()))
            query.append(" and obj.nodeType =:nodeType").setParam("nodeType", filter.getNodeType());
        if (filter.getPlanTime() != null && !"".equals(filter.getPlanTime()))
            query.append(" and obj.planTime =:planTime").setParam("planTime", filter.getPlanTime());
        if (filter.getFactTime() != null && !"".equals(filter.getFactTime()))
            query.append(" and obj.factTime =:factTime").setParam("factTime", filter.getFactTime());
        if (filter.getPlanBeginDate() != null && !"".equals(filter.getPlanBeginDate()))
            query.append(" and obj.planBeginDate =:planBeginDate").setParam("planBeginDate", filter.getPlanBeginDate());
        if (filter.getFactFinishDate() != null && !"".equals(filter.getFactFinishDate()))
            query.append(" and obj.factFinishDate =:factFinishDate").setParam("factFinishDate", filter.getFactFinishDate());
        if (filter.getStatus() != null && !"".equals(filter.getStatus()))
            query.append(" and obj.status =:status").setParam("status", filter.getStatus());
        if (filter.getState() != null && !"".equals(filter.getState()))
            query.append(" and obj.state =:state").setParam("state", filter.getState());
        if (filter.getNumber() != null && !"".equals(filter.getNumber()))
            query.append(" and obj.number =:number").setParam("number", filter.getNumber());
        if (filter.getStartTime() != null && !"".equals(filter.getStartTime()))
            query.append(" and obj.createDate >=:startTime").setParam("startTime", filter.getStartTime());
        if (filter.getEndTime() != null && !"".equals(filter.getEndTime()))
            query.append(" and obj.createDate <=:endTime").setParam("endTime", filter.getEndTime());
        if (filter.getProjectIds() != null && !"".equals(filter.getProjectIds()))
            query.append(" and project_id in(" + filter.getProjectIds() + ")");
        query.append(" order by obj.createDate DESC ");

        System.out.println("count1=" + query.listResult().size());
        return query.listResult(filter);
    }


    /**
     * 统计各部门预警次数
     * @return
     */
    public List<EarlyWarningCount> queryCalculationDept() {
        String sql = "select res_dept resDept ,count(1) count from t_bnp_early_warning_node GROUP BY res_dept";
        SQLQuery query = getSession().createSQLQuery(sql);
        query.setResultTransformer(Transformers.aliasToBean(EarlyWarningCount.class));

        List<EarlyWarningCount> res =( List<EarlyWarningCount>) query.list();
        return res;
    }

    /**
     * 统计各节点预警次数
     * @return
     */
    public List<EarlyWarningCount> queryCalculationNodeId() {
        String sql = "select node_id nodeId ,count(1) count from t_bnp_early_warning_node GROUP BY node_id";
        SQLQuery query = getSession().createSQLQuery(sql);
        query.setResultTransformer(Transformers.aliasToBean(EarlyWarningCount.class));

        List<EarlyWarningCount> res =( List<EarlyWarningCount>) query.list();
        return res;
    }

}
